<template>
  <div class="box">
    <div class="box-header" :title="hintText">
      <span class="title pos-left">{{title}}</span>
      <ul class="tools pos-right">
        <li class="item">
          <button class="btn" type="button" @click="showSettingWindow">
            <i class="icon icon-setting"></i>
          </button>
        </li>
      </ul>
    </div>
    <div class="box-body">
      <div class="chart fullsize" id="chart-4"></div>
      <div class="chartSetting" v-show="showSetting">
        <div class="form">
          <div class="form-head">
            <div class="title">选择时间范围</div>
          </div>
          <div class="form-body">
            <div class="form-item">
              <div class="label">开始时间</div>
              <div class="field">
                <input
                  type="date"
                  class="datepicker"
                  v-model="startTime"
                  :max="timeLimit"
                  :min="timeMin"
                >
              </div>
            </div>
            <div class="form-item">
              <div class="label">结束时间</div>
              <div class="field">
                <input
                  type="date"
                  class="datepicker"
                  v-model="endTime"
                  :max="timeLimit"
                  :min="timeMin"
                >
              </div>
            </div>
          </div>
          <div class="form-foot">
            <button type="button" @click="saveSetting" class="btn bg-green">
              <span class="text">确定</span>
            </button>
            <button type="button" @click="cancelSetting" class="btn bg-green">
              <span class="text">关闭</span>
            </button>
          </div>
        </div>
      </div>
      <x-loading :show="showLoading"></x-loading>
      <dl-Accept ref="dialog" :show="showModal" :take="keyModal"></dl-Accept>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import echarts from "echarts";
import Loading from "@/components/Loading";
import Pager from "@/components/Pager";
import dlAccept from "@/view/onepic/dl_Accept";

export default {
  data() {
    return {
      startTime: this.dateShift(0) + ' 00:00:00',
      endTime: this.dateShift(0)+' 23:59:59',
      timeLimit: this.dateShift(0) + ' 23:59:59',
      timeMin: this.dateShift(-365*2) + '00:00:00',
      showSetting: false,
      showLoading: false,
      chartInstance: null,
      option: null,
      params: null,
      showModal: false,
      keyModal: null,
      chartData: null,
      autoflip: false,
      ticketfn: null
    };
  },
  props: {
    title: String
  },
  components: {
    "x-loading": Loading,
    "x-pager": Pager,
    "dl-Accept": dlAccept
  },
  computed: {
    hintText() {
      return `显示数据自${this.startTime}起，截止至${this.endTime}`;
    }
  },
  mounted() {
    this.renderChart();
  },
  methods: {
    showSettingWindow() {
      this.showSetting = true;
    },
    cancelSetting() {
      this.showSetting = false;
    },
    saveSetting() {
      this.renderChart();
      this.showSetting = false;
    },
    renderChart() {
      let $this = this;
      let params = {
        startTime: this.startTime,
        endTime: this.endTime
      };
      this.showLoading = true;
      axios
        .get(this.URLHEAD + "YZT_JCJSDFX", {
          params: params
        })
        .then(res => {
          let data = res.data;
          let min = 0;

          this.option = {
            tooltip: {
              trigger: "axis",
              axisPointer: {
                type: "shadow"
              },
              formatter: function(params) {
                var tooltipStr = params[0].name;

                for (let index = 0; index < params.length; index++) {
                  tooltipStr += '<br />' + params[index].seriesName + ': ' + params[index].data;

                  switch (params[index].seriesName) {
                    case '接警量':
                    tooltipStr += '起';
                      break;
                      case '消防员人数':
                    tooltipStr += '人';
                      break;
                  }
                }

                return tooltipStr;
              }
            },
            legend: {
              data: ["接警量", "消防员人数"],
              align: "right",
              right: 10,
              textStyle: {
                color: "#fff"
              },
              itemWidth: 10,
              itemHeight: 10,
              itemGap: 35
            },
            grid: {
              left: "3%",
              right: "3%",
              bottom: "7%",
              top: 60,
              containLabel: true,
              z: 22
            },
            xAxis: [
              {
                type: "category",
                gridIndex: 0,
                axisTick: {
                  alignWithLabel: true
                },
                axisLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.7)"
                  }
                },
                axisLabel: {
                  show: true,
                  color: "#a4a7b5",
                  fontSize: 12,
                  rotate: 45,
                  interval: 0
                },
                data: data.Data.list.map(o => {
                  return o.AreaName;
                })
              }
            ],
            yAxis: [
              {
                name: '警情(起)',
                type: "value",
                gridIndex: 0,
                splitLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.3)"
                  }
                },
                axisTick: {
                  show: false
                },
                axisLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.7)"
                  }
                },
                axisLabel: {
                  color: "rgba(255,255,255,0.7)",
                  formatter: "{value}"
                }
              },
               {
                name: '人数(个)',
                type: "value",
                position: 'right',
                gridIndex: 0,
                splitLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.3)"
                  }
                },
                axisTick: {
                  show: false
                },
                axisLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.7)"
                  }
                },
                axisLabel: {
                  color: "rgba(255,255,255,0.7)",
                  formatter: "{value}人"
                }
              },
              {
                type: "value",
                gridIndex: 0,
                splitNumber: 12,
                splitLine: {
                  show: false
                },
                axisLine: {
                  show: false
                },
                axisTick: {
                  show: false
                },
                axisLabel: {
                  show: false
                },
                splitArea: {
                  show: true,
                  areaStyle: {
                    color: ["rgba(250,250,250,0.0)", "rgba(250,250,250,0.05)"]
                  }
                }
              }
            ],
            series: [
              {
                name: "接警量",
                type: "bar",
                barWidth: "20%",
                barGap: "15%",
                xAxisIndex: 0,
                yAxisIndex: 0,
                itemStyle: {
                  normal: {
                    barBorderRadius: 30,
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: this.COLORS[4]
                      },
                      {
                        offset: 1,
                        color: this.COLORS[5]
                      }
                    ])
                  }
                },
                label: {
                  normal: {
                    show: true,
                    position: "top",
                    color: this.COLORS[4]
                  }
                },
                data: data.Data.list.map(o => {
                  return o.JJCount;
                }),
                zlevel: 11
              },
              {
                name: "消防员人数",
                type: "bar",
                barWidth: "20%",
                barGap: "15%",
                xAxisIndex: 0,
                yAxisIndex: 0,
                itemStyle: {
                  normal: {
                    barBorderRadius: 30,
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: "#00feff"
                      },
                      {
                        offset: 0.5,
                        color: "#027eff"
                      },
                      {
                        offset: 1,
                        color: "#0286ff"
                      }
                    ])
                  }
                },
                label: {
                  normal: {
                    show: true,
                    position: "top",
                    color: "#00feff",
                  }
                },
                data: data.Data.list.map(o => {
                  return o.XFYCount;
                }),
                zlevel: 11
              }
            ]
          };

          this.chartInstance = this.$Crender("chart-4", this.option);
          this.chartInstance.on("click", e => {
            let teamName = e.name;
            $this.keyModal = teamName;
            $this.$refs.dialog.query(teamName);
          });
          $this.showLoading = false;
        });
    }
  }
};
</script>
<style scoped>
</style>
